<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="板块名称:">
        <el-input v-model="queryParams.panelName" size="small" placeholder="请输入板块名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:panel:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:panel:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['marketing:panel:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="板块名称" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <router-link :to="'/panelContent/list/' + scope.row.id" class="link-type">
            <span>{{ scope.row.panelName }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="板块类型" align="center" prop="panelType">
        <template slot-scope="scope">
          <span>{{ panelTypeFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容数量限制" align="center" prop="limitQty" />
      <el-table-column label="所属位置" align="center" prop="panelPosition">
        <template slot-scope="scope">
          <span>{{ panelPositionFormat(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['marketing:panel:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['marketing:panel:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="panelFormDialog.title" :visible.sync="panelFormDialog.visible" :close-on-click-modal="false" :before-close="resetPanelFormDialog" width="500px" append-to-body>
      <el-form ref="panelForm" :model="panelFormData" :rules="panelFormRules" label-width="100px">
        <el-form-item label="板块名称" prop="panelName">
          <el-input v-model="panelFormData.panelName" placeholder="请输入板块名称" />
        </el-form-item>
        <el-form-item label="板块类型" prop="panelType">
          <el-select v-model="panelFormData.panelType" style="width: 100%" clearable placeholder="请选择板块类型">
            <el-option v-for="option in options.panelTypeOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属位置" prop="panelPosition">
          <el-select v-model="panelFormData.panelPosition" style="width: 100%" clearable placeholder="请选择所属位置">
            <el-option v-for="option in options.panelPositionOptions" :key="option.dictValueCode" :label="option.dictValueName" :value="option.dictValueCode" />
          </el-select>
        </el-form-item>
        <el-form-item label="内容数量限制" prop="limitQty">
          <el-input v-model="panelFormData.limitQty" type="number" placeholder="请输入内容数量限制" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="panelFormData.sort" type="number" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmPanelFormDialog">确 定</el-button>
        <el-button @click="resetPanelFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PanelApi from '@/api/marketing/PanelApi'
import DictValueApi from '@/api/system/DictValueApi'

const defaultPanelFormData = {
  panelName: null,
  panelType: null,
  limitQty: null,
  panelPosition: null,
  sort: 10,
  status: '0'
}
export default {
  name: 'PanelList',
  data() {
    return {
      options: {
        panelTypeOptions: [], // 板块类型字典
        panelPositionOptions: [], // 所属位置字典
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        panelName: null,
        panelType: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      panelFormDialog: {
        title: '',
        visible: false
      },
      panelFormData: Object.assign({}, defaultPanelFormData),
      panelFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      DictValueApi.list({ dictCode: 'mms_panel_type' }).then(response => {
        this.options.panelTypeOptions = response.data
      })
      DictValueApi.list({ dictCode: 'mms_panel_position' }).then(response => {
        this.options.panelPositionOptions = response.data
      })
    },
    handleAdd() {
      this.openPanelFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return PanelApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openPanelFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      PanelApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return PanelApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openPanelFormDialog(id) {
      this.panelFormDialog.visible = true
      if (id) {
        this.panelFormDialog.title = '活动板块-修改'
        PanelApi.get(id).then(response => {
          this.panelFormData = {
            ...this.panelFormData,
            ...response.data
          }
        })
      } else {
        this.panelFormDialog.title = '活动板块-添加'
      }
    },
    confirmPanelFormDialog() {
      this.$refs['panelForm'].validate(valid => {
        if (valid) {
          if (this.panelFormData.id) {
            PanelApi.edit(this.panelFormData).then(response => {
              this.$message.success('修改成功')
              this.resetPanelFormDialog()
              this.handleQuery()
            })
          } else {
            PanelApi.add(this.panelFormData).then(response => {
              this.$message.success('添加成功')
              this.resetPanelFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetPanelFormDialog() {
      this.panelFormDialog = {
        title: '',
        visible: false
      }
      this.panelFormData = Object.assign({}, defaultPanelFormData)
      this.resetForm('panelForm')
    },
    // 板块类型字典
    panelTypeFormat(row) {
      return this.$common.getDictValueName(this.options.panelTypeOptions, row.panelType)
    },
    // 所属位置字典
    panelPositionFormat(row) {
      return this.$common.getDictValueName(this.options.panelPositionOptions, row.position)
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
